﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

using System.Data;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class GPSTrackingClientsWebsiteManager : System.Web.UI.MasterPage
    {    

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Menu.Width = Unit.Percentage(100);
                string Username = HttpContext.Current.User.Identity.Name;

                Menu.Items[0].Text = "Bienvenido(a): " + Username;
            }
        }
       

        protected void GeoFenceGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GeoFenceGrid.DataSource = new System.Data.DataTable();
        }

        protected void btnLoginOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Abandon();

            Response.Redirect("../../Default.aspx", true);
        }

        protected void GeoFenceGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem Item = e.Item as GridDataItem;
            int GroupID = Convert.ToInt32(Item.GetDataKeyValue("GroupID"));
            string Username = Request.QueryString["Username"];
            GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();

            try
            {
                transactionManager.BeginTransaction();

                //Imei
                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GroupsByImei> groupsByImeiEntites = GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.GetByGroupId(GroupID);
                foreach (GPSTrackingServerDB.Entities.GroupsByImei entity in groupsByImeiEntites)
                {
                    entity.OriginalGroupId = entity.GroupId;
                    entity.OriginalImei = entity.Imei;

                    entity.GroupId = 1; //UnCategory
                    entity.EntityState = GPSTrackingServerDB.Entities.EntityState.Changed;


                    GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Update(transactionManager, entity);
                }

                //Delete Groups by User
                GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.Delete(transactionManager, Username, GroupID);


                //Delete group name
                GPSTrackingServerDB.Data.DataRepository.GroupsProvider.Delete(transactionManager, GroupID);

                transactionManager.Commit();

                //Refresh
            }
            catch (Exception ex)
            {
                transactionManager.Rollback();
                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }
        }

        protected void rgCustomers_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
                //GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFence> geoFenceEntities = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFence>();

                //rgCustomersGrid.DataSource = geoFenceEntities;


                // Create a new DataTable.
                System.Data.DataTable table = new DataTable("ParentTable");
                // Declare variables for DataColumn and DataRow objects.
                DataColumn column;
                DataRow row;

                // Create new DataColumn, set DataType, 
                // ColumnName and add to DataTable.    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int64");
                column.ColumnName = "CustomerID";
                column.ReadOnly = true;
                column.Unique = true;
                // Add the Column to the DataColumnCollection.
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "CustomerName";
                //column.AutoIncrement = false;
                column.Caption = "CustomerName";
                column.ReadOnly = true;
                //column.Unique = false;
                // Add the column to the table.
                table.Columns.Add(column);

                // Make the ID column the primary key column.
                DataColumn[] PrimaryKeyColumns = new DataColumn[1];
                PrimaryKeyColumns[0] = table.Columns["CustomerID"];
                table.PrimaryKey = PrimaryKeyColumns;

                // Instantiate the DataSet variable.
                DataSet dataSet = new DataSet();
                // Add the new DataTable to the DataSet.
                dataSet.Tables.Add(table);

                // Create three new DataRow objects and add 
                // them to the DataTable
                /*row = table.NewRow();
                row["id"] = 0;
                row["route"] = "Ruta San Joaquin";
                table.Rows.Add(row);

                row = table.NewRow();
                row["id"] = 1;
                row["route"] = "Ruta San Pedro";
                table.Rows.Add(row);

                row = table.NewRow();
                row["id"] = 2;
                row["route"] = "Ruta Coronado";
                table.Rows.Add(row);
                */

                rgCustomersGrid.DataSource = dataSet;
        }

        protected void rgCustomersGroupsGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgCustomersGroupsGrid.DataSource = getDataSetForCustomerGroupsGrid();
        }

        protected DataSet getDataSetForCustomerGroupsGrid()
        {
            System.Data.DataTable table = new DataTable("ParentTable");
            // Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int64");
            column.ColumnName = "CustomerGroupID";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CustomerGroupName";
            //column.AutoIncrement = false;
            column.Caption = "CustomerGroupName";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Make the ID column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["CustomerGroupID"];
            table.PrimaryKey = PrimaryKeyColumns;

            // Instantiate the DataSet variable.
            DataSet dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);

            return dataSet;
        }

        protected void rgUsersRoutesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            System.Data.DataTable table = new DataTable("ParentTable");
            // Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Username";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Name";
            //column.AutoIncrement = false;
            column.Caption = "Name";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "LastName";
            //column.AutoIncrement = false;
            column.Caption = "LastName";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Make the ID column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["Username"];
            table.PrimaryKey = PrimaryKeyColumns;

            // Instantiate the DataSet variable.
            DataSet dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);

            rgUsersRoutesGrid.DataSource = dataSet;
            
        }

        protected void rgGpsRoutesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            System.Data.DataTable table = new DataTable("ParentTable");
            // Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Imei";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Name";
            //column.AutoIncrement = false;
            column.Caption = "Name";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Make the ID column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["Imei"];
            table.PrimaryKey = PrimaryKeyColumns;

            // Instantiate the DataSet variable.
            DataSet dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);

            rgGpsRoutesGrid.DataSource = dataSet;
        }

        protected void rgCustomersGroupsRoutesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgCustomersGroupsRoutesGrid.DataSource = getCustomersGroupsRoutes();
        }

        protected DataSet getCustomersGroupsRoutes(){
                    System.Data.DataTable table = new DataTable("ParentTable");
            // Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int64");
            column.ColumnName = "CustomerID";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int64");
            column.ColumnName = "NumPos";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "KmsText";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "DurationText";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CustomerName";
            //column.AutoIncrement = false;
            column.Caption = "CustomerName";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "CustomerGroupName";
            //column.AutoIncrement = false;
            column.Caption = "CustomerGroupName";
            column.ReadOnly = true;
            //column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Make the ID column the primary key column.
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["CustomerID"];
            table.PrimaryKey = PrimaryKeyColumns;

            // Instantiate the DataSet variable.
            DataSet dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);
            
            return dataSet;
        }

        

        protected void rgCustomersGroupsRoutesSelectedGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgCustomersGroupsRoutesSelectedGrid.DataSource = getCustomersGroupsRoutes();
        }

    }
}